package com.miracle.main.module.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.miracle.main.module.di.component.DaggerFileComponent;
import com.miracle.basemvp.base.BaseActivity;
import com.miracle.basemvp.base.BaseFragment;
import com.miracle.basemvp.di.component.AppComponent;
import com.miracle.common.util.FilePickUtil;
import com.miracle.main.R;
import com.miracle.main.R2;
import com.miracle.main.module.di.module.FileModule;
import com.miracle.main.module.mvp.contract.FileContract;
import com.miracle.main.module.mvp.contract.UploadContract;
import com.miracle.main.module.mvp.presenter.FilePresenter;
import com.miracle.main.module.mvp.presenter.UpdateInfoPresenter;
import com.miracle.main.module.mvp.ui.fragment.FileListFragment;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * com.miracle.main.module.mvp.ui.activity
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/11 13:18
 * @see com.miracle.main.module.mvp.ui.activity
 */
public class UploadActivity extends BaseActivity<FilePresenter> implements FileContract
        .View {

    @BindView(R2.id.tv_title)
    TextView mTvTitle;

    LifecycleProvider<ActivityEvent> lifecycleProvider;

    public static void start(BaseActivity context) {
        Intent intent = new Intent(context, UploadActivity.class);
        context.showActivity(intent);
    }

    @Override
    public void showData(Object o) {

    }

    @Override
    public int setLayoutId(@Nullable Bundle bundle) {
        return R.layout.cloud_activity_upload_file;
    }

    @Override
    public void initView() {
        mTvTitle.setText("添加文件");
    }

    @Override
    public void initData() {

    }

    @Override
    public void setDaggerComponent(AppComponent appComponent) {
        DaggerFileComponent
                .builder()
                .appComponent(appComponent)
                .fileModule(new FileModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onRequest(LifecycleProvider<ActivityEvent> lifecycleProvider) {
        this.lifecycleProvider = lifecycleProvider;
    }

    @OnClick({R2.id.iv_back, R2.id.tv_submit})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            onBackPressed();
        }

        if (id == R.id.tv_submit) {
            FilePickUtil.startFilePickView(this);
//            FilePickUtil.showFileChooser(this);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1000) {
                //If it is a file selection mode, you need to get the path collection of all the
                // files selected
                //List<String> list = data.getStringArrayListExtra(Constant.RESULT_INFO);
                // Constant.RESULT_INFO == "paths"
                List<String> list = data.getStringArrayListExtra("paths");
                Toast.makeText(getApplicationContext(), "selected " + list.size(), Toast
                        .LENGTH_SHORT).show();
                //If it is a folder selection mode, you need to get the folder path of your choice
                String path = data.getStringExtra("path");
                Toast.makeText(getApplicationContext(), "The selected path is:" + path, Toast
                        .LENGTH_SHORT).show();
                mPresenter.uploadFiles(lifecycleProvider,list);

            }
        }
    }

    private void testObsever() {

        Observable.create(new ObservableOnSubscribe<String[]>() {


            @Override
            public void subscribe(ObservableEmitter<String[]> e) throws Exception {
                String str[] = {"文本", "图片"};
                e.onNext(str);
            }
        }).map(new Function<String[], ArrayList<Fragment>>() {
            @Override
            public ArrayList<Fragment> apply(String[] strings) throws Exception {
                ArrayList<Fragment> list = new ArrayList<>();
                for (int i = 0; i < strings.length; i++) {
                    list.add(FileListFragment.newInstance(i));
                }
                return list;
            }
        }).subscribe(new Consumer<ArrayList<Fragment>>() {
            @Override
            public void accept(ArrayList<Fragment> fragments) throws Exception {

            }
        });
    }
}
